--lua script
--#include "data\language\LangCode.txt" once   --语言包
--#include "data\config\ride\RideGameConfig.lua" once

--屏蔽坐骑
--[[
--坐骑进阶
function OnUpRideLevelOp(sysarg,args)
	local nType = args[2]

	local nLevel = Actor.getRideOrder(sysarg)
	local nStar = Actor.getRideStarIndex(sysarg)

	if RideGameConfig.rideRankConfig[nLevel+1] == nil then return end
	if (nLevel + 1) == table.getn(RideGameConfig.rideRankConfig) and nStar == 10 then
		Actor.sendTipmsg( sysarg, Lang.Gem.t00040,ttFlyTip)
		return 
	end 

	local nIndex = 0
	if nLevel==0 then	--0阶0星
		nIndex = nStar + 1
	else
		nIndex = nStar
	end

	if RideGameConfig.rideRankConfig[nLevel+1].starOrder[nIndex] == nil then return end

	local nRideConfig = RideGameConfig.rideRankConfig[nLevel+1].starOrder[nIndex]

	if nType == 1 then		--荣誉提升
		OnUpByHonourOp(sysarg,nRideConfig,RideGameConfig.rideRankConfig[nLevel+1].exprate)
	elseif nType == 2 then		--元宝提升
		OnUpLevelByYbOp(sysarg,nRideConfig,RideGameConfig.rideRankConfig[nLevel+1].exprate)
	elseif nType == 3 then		--一键提升
		OnekeyUpRideLevel(sysarg)
	end
end

--荣誉提升
function OnUpByHonourOp(sysarg,nRideConfig,exprate)
	if nRideConfig == nil then return end
	
	local svar = Actor.getStaticVar(sysarg)

	if svar.UpByHonourTimes == nil then
		svar.UpByHonourTimes = 0
	end

	local nHonour = Actor.getIntProperty(sysarg,PROP_ACTOR_HONOUR)
	if nHonour < nRideConfig.UpLevelType[1].value then 
		Actor.sendTipmsg( sysarg, string.format(Lang.Gem.t00031,nRideConfig.UpLevelType[1].value),ttFlyTip)
		return
	end

	Actor.giveAward(sysarg,21,0,-nRideConfig.UpLevelType[1].value,0,0,0)

	local addExp = nRideConfig.UpLevelType[1].AwardExp
	
	svar.UpByHonourTimes = svar.UpByHonourTimes + 1

	if svar.UpByHonourTimes > nRideConfig.UpLevelType[1].minAwardTimes[1] then
		local nRandNum = System.getRandomNumber(10000)
		if nRandNum < nRideConfig.UpLevelType[1].minAwardTimes[2] then
			local pack = DataPack.allocPacket(sysarg, 42, 2)
			if (pack ~= nil) then
				DataPack.writeChar(pack, 1)
				DataPack.flush(pack)
			end

			addExp = addExp * exprate
			svar.UpByHonourTimes = 0
		end
	end


	AddRideExp(sysarg,addExp,nRideConfig)
	Actor.triggerQuestEvent(sysarg, 127, 50100, 1 )
end

--元宝提升
function OnUpLevelByYbOp(sysarg,nRideConfig,exprate)
	if nRideConfig == nil then return end
	
	local svar = Actor.getStaticVar(sysarg)

	if svar.UpLevelByHonourTimes == nil then
		svar.UpLevelByHonourTimes = 0
	end

	local itemPtr = Item.getBagItemPtrById(sysarg,nRideConfig.UpLevelType[3].value)

	if itemPtr then		--背包有快马鞭
		Item.removeItem(sysarg,itemPtr,1,"",5)
	else
		local myYb = Actor.getIntProperty(sysarg,PROP_ACTOR_YUANBAO)

		if myYb < nRideConfig.UpLevelType[2].value then
			Actor.sendTipmsg( sysarg, string.format(Lang.Gem.t00033,nRideConfig.UpLevelType[2].value),ttFlyTip)
			return
		end

		Actor.giveAward(sysarg,15,0,-nRideConfig.UpLevelType[2].value,0,0,0,0,162,Lang.Gem.t00034)
	end

	local addExp = nRideConfig.UpLevelType[2].AwardExp
	svar.UpLevelByHonourTimes = svar.UpLevelByHonourTimes + 1

	if svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].minAwardTimes[1] or svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].maxAwardTimes[1] then
		local nRandNum = System.getRandomNumber(10000)
		
		local nType = 0
		if nRandNum < nRideConfig.UpLevelType[2].maxAwardTimes[2] and svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].maxAwardTimes[1] then
			local maxExp = nRideConfig.needExp
			local curExp = Actor.getIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP)
			addExp = maxExp - curExp

			nType = 2
		elseif nRandNum < nRideConfig.UpLevelType[2].minAwardTimes[2] and svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].minAwardTimes[1] then
			addExp = addExp * exprate
			nType = 1
		end

		if nType > 0 then
			local pack = DataPack.allocPacket(sysarg, 42, 2)
			if (pack ~= nil) then
				DataPack.writeChar(pack, nType)
				DataPack.flush(pack)
			end
			svar.UpLevelByHonourTimes = 0
		end
	end


	AddRideExp(sysarg,addExp,nRideConfig)
	Actor.triggerQuestEvent(sysarg, 127, 50100, 1 )
end


--给坐骑加经验
function AddRideExp(sysarg,nValue,nRideConfig)
	if nValue <= 0 then return end
	
	local maxExp = nRideConfig.needExp
	local curExp = Actor.getIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP)
	
	local nOldOrder = Actor.getRideOrder(sysarg)
	local nOldLevel = Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL)	
	if nValue + curExp >= maxExp then
		local nOrder = Actor.getRideOrder(sysarg)
		local nStar = Actor.getRideStarIndex(sysarg)
		
		local curValue = nValue + curExp
		while curValue >= maxExp do
			local nLevel = Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL)
			Actor.setUIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL,nLevel+1)
			
			if (nOrder + 1) == table.getn(RideGameConfig.rideRankConfig) and nStar == 9 then
				Actor.setUIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP,maxExp)
				break
			else
				local nIndex = Actor.getRideOrder(sysarg)
				local nStarIndex = Actor.getRideStarIndex(sysarg)
				local tmpConfig = RideGameConfig.rideRankConfig[nIndex+1].starOrder[nStarIndex]
				Actor.setUIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP,nValue + curExp - maxExp)
				curValue = nValue + curExp - maxExp
				maxExp = tmpConfig.needExp
			end 
		end
		
		if nStar == 10 then
			Actor.sendTipmsg( sysarg, Lang.Gem.t00041,ttFlyTip)
		end

		local svar = Actor.getStaticVar(sysarg)
		svar.UpLevelByHonourTimes = nil
		svar.UpByHonourTimes = nil
		Actor.collectOperate(sysarg,0)
	else
		Actor.setUIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP,nValue + curExp)
	end
	
	local nNewLevel = Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL)	
	if nNewLevel > nOldLevel then
		System.sendCommonLog(903,Actor.getIntProperty(sysarg,PROP_ENTITY_ID),Actor.getName(sysarg),nOldLevel,nNewLevel)
	end

	Actor.sendTipmsg( sysarg, string.format(Lang.Gem.t00032,nValue),ttFlyTip)
	local nNewOrder = Actor.getRideOrder(sysarg)
	if nNewOrder > nOldOrder then
		local rideName = System.getRideNameByLevel(Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL))
		if rideName then
			local str2 = string.format(Lang.GuildLang.m00165,Actor.getName(sysarg),nNewOrder,rideName)
			System.broadcastTipmsg(str2,2+128)
		end
	end
end

--一键提升
function OnekeyUpRideLevel(sysarg)
	if Actor.isVip(sysarg) == false then
		Actor.sendTipmsg( sysarg, Lang.Gem.t00044,ttFlyTip)
		return
	end

	local myYb = Actor.getIntProperty(sysarg,PROP_ACTOR_YUANBAO)

	if myYb < RideGameConfig.oneKeyNeedYb then
		Actor.sendTipmsg( sysarg, Lang.Gem.t00046,ttFlyTip)
		return
	end

	local nOldOrder = Actor.getRideOrder(sysarg)
	Actor.giveAward(sysarg,15,0,-RideGameConfig.oneKeyNeedYb,0,0,0,0,162,Lang.Gem.t00045)
	
	local resultLev = Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL)			--坐骑一键提升后的等级
	local resultExp = Actor.getIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP)			--坐骑一键提升后的经验
	local totalExp = 0			--坐骑一键提升总共获得经验
	local minTimes = 0			--小惊喜的次数
	local maxTimes = 0			--大惊喜的次数 

	local nLevel = Actor.getRideOrder(sysarg)
	local nStar = Actor.getRideStarIndex(sysarg)

	local nRideLevel = Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL)

	if (nLevel + 1) == table.getn(RideGameConfig.rideRankConfig) and nStar == 10 then
		Actor.sendTipmsg( sysarg, Lang.Gem.t00040,ttFlyTip)
		return 
	end 

	local svar = Actor.getStaticVar(sysarg)

	if svar.UpLevelByHonourTimes == nil then
		svar.UpLevelByHonourTimes = 0
	end

	for i=0,RideGameConfig.oneKeyUpTimes do
		nLevel =GetRideOderByLevel(nRideLevel)
		nStar = GetRideStarByLevel(nRideLevel)
		
		if RideGameConfig.rideRankConfig[nLevel+1] == nil then 
			break
		end

		if (nLevel + 1) == table.getn(RideGameConfig.rideRankConfig) and nStar == 10 then
			break 
		end 

		local nIndex = 0
		if nLevel==0 then	--0阶0星
			nIndex = nStar + 1
		else
			nIndex = nStar
		end

		if RideGameConfig.rideRankConfig[nLevel+1].starOrder[nIndex] == nil then 
			break
		end
		
		local nRideConfig = RideGameConfig.rideRankConfig[nLevel+1].starOrder[nIndex]
		local addExp = nRideConfig.UpLevelType[2].AwardExp

		svar.UpLevelByHonourTimes = svar.UpLevelByHonourTimes + 1
		if svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].minAwardTimes[1] or svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].maxAwardTimes[1] then
			local nRandNum = System.getRandomNumber(10000)
			
			local nType = 0
			if nRandNum < nRideConfig.UpLevelType[2].maxAwardTimes[2] and svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].maxAwardTimes[1] then
				local maxExp = nRideConfig.needExp
				local curExp = resultExp
				addExp = maxExp - curExp
				maxTimes = maxTimes + 1
				nType = 2
			elseif nRandNum < nRideConfig.UpLevelType[2].minAwardTimes[2] and svar.UpLevelByHonourTimes > nRideConfig.UpLevelType[2].minAwardTimes[1] then
				addExp = addExp * RideGameConfig.rideRankConfig[nLevel+1].exprate
				nType = 1
				minTimes = minTimes + 1
			end

			if nType > 0 then
				svar.UpLevelByHonourTimes = 0
			end
		end

		local maxRideExp = nRideConfig.needExp

		if addExp + resultExp >= maxRideExp then			
			local curValue = addExp + resultExp
			while curValue >= maxRideExp do
				nRideLevel = nRideLevel + 1
				
				if (nLevel + 1) == table.getn(RideGameConfig.rideRankConfig) and nStar == 9 then
					resultExp = maxRideExp
					break
				else
					local nRideIndex = GetRideOderByLevel(nRideLevel)
					local nStarIndex = GetRideStarByLevel(nRideLevel)
					local tmpConfig = RideGameConfig.rideRankConfig[nRideIndex+1].starOrder[nStarIndex]
					curValue = curValue - maxRideExp
					if tmpConfig then
						maxRideExp = tmpConfig.needExp
					end
				end 
			end

			resultExp = curValue
			totalExp = totalExp + addExp

			svar.UpLevelByHonourTimes = 0
			svar.UpByHonourTimes = 0
		else
			totalExp = totalExp + addExp
			resultExp = resultExp + addExp
		end
	end
	
	local nOldLevel = Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL)	
	if nRideLevel > nOldLevel then
		System.sendCommonLog(903,0,"",nOldLevel,nRideLevel)
	end

	Actor.setUIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL,nRideLevel)
	Actor.setUIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP,resultExp)

	local RateExp = 0
	local nRateCount = RideGameConfig.oneKeyRate[2]
	if nRateCount > 0 then
		local nRateRandNum = System.getRandomNumber(nRateCount) + 1
		if nRateRandNum > nRateCount then
			nRateRandNum = nRateCount
		end
		
		if nRateRandNum <= 0 then
			nRateRandNum =  RideGameConfig.oneKeyRate[1]
		end
		
		RateExp = math.floor(totalExp * nRateRandNum / 100)
	end
	
	local pack = DataPack.allocPacket(sysarg, 42, 3)
	if (pack ~= nil) then
		DataPack.writeInt(pack, totalExp)
		DataPack.writeInt(pack, minTimes)
		DataPack.writeInt(pack, maxTimes)
		DataPack.writeInt(pack, RateExp)
		DataPack.flush(pack)
	end

	Actor.collectOperate(sysarg,0)

	local nNewOrder = Actor.getRideOrder(sysarg)
	if nNewOrder > nOldOrder then
		local rideName = System.getRideNameByLevel(Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL))
		if rideName then
			local str2 = string.format(Lang.GuildLang.m00165,Actor.getName(sysarg),nNewOrder,rideName)
			System.broadcastTipmsg(str2,2+128)
		end
	end
	Actor.triggerQuestEvent(sysarg, 127, 50100, 1 )
end

--给坐骑加经验
function OnRideAddExp(nValue)
	local nOrder = Actor.getRideOrder(sysarg)
	local nStar = Actor.getRideStarIndex(sysarg)

	
	if RideGameConfig.rideRankConfig[nOrder+1] == nil then 
		return
	end

	if (nOrder + 1) == table.getn(RideGameConfig.rideRankConfig) and nStar == 10 then
		return 
	end 

	local nIndex = 0
	if nOrder==0 then	--0阶0星
		nIndex = nStar + 1
	else
		nIndex = nStar
	end

	if RideGameConfig.rideRankConfig[nLevel+1].starOrder[nIndex] == nil then 
		return
	end
	
	local nRideConfig = RideGameConfig.rideRankConfig[nLevel+1].starOrder[nIndex]
	local maxExp = nRideConfig.needExp
	local curExp = Actor.getIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP)

	if nValue + curExp >= maxExp then
		local nLevel = Actor.getRideOrder(sysarg)
		local curStar = Actor.getRideStarIndex(sysarg)
		
		local curValue = nValue + curExp
		while curValue >= maxExp do
			local tmpLevel = Actor.getIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL)
			Actor.setUIntProperty(sysarg,PROP_ACTOR_RIDE_LEVEL,tmpLevel+1)
			
			if (nLevel + 1) == table.getn(RideGameConfig.rideRankConfig) and curStar == 9 then
				Actor.setUIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP,maxExp)
				break
			else
				local tmpIndex = Actor.getRideOrder(sysarg)
				local nStarIndex = Actor.getRideStarIndex(sysarg)
				local tmpConfig = RideGameConfig.rideRankConfig[tmpIndex+1].starOrder[nStarIndex]
				Actor.setUIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP,nValue + curExp - maxExp)
				curValue = nValue + curExp - maxExp
				maxExp = tmpConfig.needExp
			end 
		end
	else
		Actor.setUIntProperty(sysarg,PROP_ACTOR_MOUNT_EXP,nValue + curExp)
	end

end

--获得阶
function GetRideOderByLevel(nLevel)
	local nResult = 0
	if nLevel==0 then
		nResult = 0
	else
		nResult = math.floor((nLevel-1)/10)
	end

	return nResult
end

--获得星
function GetRideStarByLevel(nLevel)
	local nResult = 0
	if nLevel==0 then
		nResult = 0
		return nResult
	elseif nLevel>0 and (nLevel % 10) == 0 then
		nResult =  10

		return nResult
	end

	nResult =  (nLevel % 10)

	return nResult
end
--]]